// -*- c++ -*-
/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#ifndef AUDIO_PORT_IMPL_H__
#define AUDIO_PORT_IMPL_H__

#include <complex>
#include <SceOsSemaphore.h>
#include "CF__Port_impl.h"
#include "audioPortS.h"

#ifdef _WIN32
#ifndef AUDIOPORT_EXPORTS
#define AUDIOPORT_EXPORT __declspec(dllimport)
#else 
#define AUDIOPORT_EXPORT __declspec(dllexport)
#endif
#else //_WIN32
#define AUDIOPORT_EXPORT
#endif // _WIN32

const unsigned int MAX_BUF_SIZE=2048;

/*
 * ----------------------------------------------------------------------
 * Char port implementations.
 * ----------------------------------------------------------------------
 */

class AUDIOPORT_EXPORT CharOutPort_impl : public CF__Port_impl {
public:
  CharOutPort_impl (char * out_buf, unsigned long max_data_size);
  virtual ~CharOutPort_impl ();

  // push data out the output data port
  void pushData (char *buffer, unsigned long count);

  CORBA::Object_ptr getPort()
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

  virtual void connectPort (CORBA::Object_ptr connection,
			    const CORBA::Char * connectionId)
    throw (CORBA::SystemException, CF::Port::InvalidPort, 
	   CF::Port::OccupiedPort);

  bool IsConnected ()
  {
    return m_already_connected;
  }

private:
  MC_SCA_CF::CharSequence *m_out_data;
  MC_SCA_CF::DataPortChar_var m_remote_port;
  bool m_already_connected;
  unsigned long m_previous_data_length;
};

class AUDIOPORT_EXPORT CharInPort_impl 
  : public virtual POA_MC_SCA_CF::DataPortChar
{
public:
  CharInPort_impl (unsigned int max_data_size);
  virtual ~CharInPort_impl();

  virtual void push_char (const MC_SCA_CF::CharSequence & in_buffer)
    throw (CORBA::SystemException);

  CORBA::Object_ptr getPort ()
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

  unsigned int GetData (char ** buffer_addr);
  void ReleaseDataBuffer (int num_read = -1);

private:
  bool m_already_connected;
  SCE::OS::Semaphore m_buffer_ready_sem;
  SCE::OS::Semaphore m_data_ready_sem;
  unsigned int m_max_data_size; // buffer size

  char * m_data_ptr;    // begnning of buffer
  char * m_input_ptr;   // write pointer
  char * m_read_ptr;    // read pointer
  unsigned int m_data_count; // number of elements in buffer
};

/*
 * ----------------------------------------------------------------------
 * Short integer port implementations.
 * ----------------------------------------------------------------------
 */

class AUDIOPORT_EXPORT ShortOutPort_impl : public CF__Port_impl {
public:
  ShortOutPort_impl (short *out_buf, unsigned long max_data_size);
  virtual ~ShortOutPort_impl ();

  // push data out the output data port
  void pushData (short * buffer, unsigned long count);

  CORBA::Object_ptr getPort ()
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

  virtual void connectPort (CORBA::Object_ptr connection,
			    const CORBA::Char * connectionId)
    throw (CORBA::SystemException, CF::Port::InvalidPort, 
           CF::Port::OccupiedPort);

  bool IsConnected ()
  {
    return m_already_connected;
  }

private:
  MC_SCA_CF::ShortSequence * m_out_data;
  MC_SCA_CF::DataPortShort_var m_remote_port;
  bool m_already_connected;
  unsigned long m_previous_data_length;
};

class AUDIOPORT_EXPORT ShortInPort_impl 
  : public virtual POA_MC_SCA_CF::DataPortShort
{
public:
  ShortInPort_impl (unsigned int max_data_size);
  virtual ~ShortInPort_impl ();

  virtual void push_short (const MC_SCA_CF::ShortSequence & in_buffer)
    throw (CORBA::SystemException);

  CORBA::Object_ptr getPort ()
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

  unsigned int GetData (short ** buffer);
  void ReleaseDataBuffer (int num_read = -1);

private:
  bool m_already_connected;
  SCE::OS::Semaphore m_buffer_ready_sem;
  SCE::OS::Semaphore m_data_ready_sem;
  unsigned long m_max_data_size;

  short * m_data_ptr;   // begnning of buffer
  short * m_input_ptr;  // write pointer
  short * m_read_ptr;   // read pointer
  unsigned int m_data_count; // number of elements in buffer
};

/*
 * ----------------------------------------------------------------------
 * Complex short integer port implementations.
 * ----------------------------------------------------------------------
 */

typedef std::complex<short> CMPLX_INT16; // complex short
typedef std::complex<float> CMPLX_FLT32; // complex float

class AUDIOPORT_EXPORT ComplexShortOutPort_impl : public CF__Port_impl {
public:
  ComplexShortOutPort_impl (CMPLX_INT16 * out_buf, unsigned long max_data_size);
  virtual ~ComplexShortOutPort_impl ();

  // push data out the output data port
  void pushData (CMPLX_INT16 * buffer, unsigned long count);

  CORBA::Object_ptr getPort ()
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

  virtual void connectPort (CORBA::Object_ptr connection,
			    const CORBA::Char * connectionId)
    throw (CORBA::SystemException, CF::Port::InvalidPort, 
           CF::Port::OccupiedPort);

  bool IsConnected ()
  {
    return m_already_connected;
  }

private:
  MC_SCA_CF::ComplexShortSequence * m_out_data;
  MC_SCA_CF::DataPortComplexShort_var m_remote_port;
  bool m_already_connected;
  unsigned long m_previous_data_length;
};

class AUDIOPORT_EXPORT ComplexShortInPort_impl 
  : public virtual POA_MC_SCA_CF::DataPortComplexShort
{
public:
  ComplexShortInPort_impl(unsigned int max_data_size);
  virtual ~ComplexShortInPort_impl();

  virtual void push_complexShort (const MC_SCA_CF::ComplexShortSequence & in_buffer)
    throw (CORBA::SystemException);

  CORBA::Object_ptr getPort ()
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

  unsigned int GetData (CMPLX_INT16 ** buffer_addr);
  void ReleaseDataBuffer (int num_read = -1);

private:
  bool m_already_connected;
  SCE::OS::Semaphore m_buffer_ready_sem;
  SCE::OS::Semaphore m_data_ready_sem;
  unsigned int m_max_data_size; // buffer size

  CMPLX_INT16 * m_data_ptr;  // begnning of buffer
  CMPLX_INT16 * m_input_ptr; // write pointer
  CMPLX_INT16 * m_read_ptr;  // read pointer
  unsigned int m_data_count; // number of elements in buffer
};

/*
 * ----------------------------------------------------------------------
 * Voice mode port definitions.
 * ----------------------------------------------------------------------
 */

// Define PCM port types.
typedef ShortInPort_impl PcmInPort_impl;
typedef ShortOutPort_impl PcmOutPort_impl;

// Define CVSD port types.
typedef CharInPort_impl CvsdInPort_impl;
typedef CharOutPort_impl CvsdOutPort_impl;

// Define FM3TR port types.
typedef CharInPort_impl Fm3trInPort_impl;
typedef CharOutPort_impl Fm3trOutPort_impl;

// Define MSK port types.
typedef ComplexShortInPort_impl MskInPort_impl;
typedef ComplexShortOutPort_impl MskOutPort_impl;

/*
 * ----------------------------------------------------------------------
 * Data mode port definitions.
 * ----------------------------------------------------------------------
 */

// Define FileIO port types.
typedef CharInPort_impl FileInPort_impl;
typedef CharOutPort_impl FileOutPort_impl;

// Define RS Block port types.
typedef CharInPort_impl RsBlockInPort_impl;
typedef CharOutPort_impl RsBlockOutPort_impl;

// Define bit packetizer port types.
typedef CharInPort_impl BitPacketizerInPort_impl;
typedef CharOutPort_impl BitPacketizerOutPort_impl;

// Define RS Hop port types.
typedef CharInPort_impl RsHopInPort_impl;
typedef CharOutPort_impl RsHopOutPort_impl;

// Define MAC port types.
typedef ShortInPort_impl MacInPort_impl;
typedef ShortOutPort_impl MacOutPort_impl;

#endif
